package com.jshoperxms.service.impl;

import com.jshoperxms.controller.utils.BaseTools;
import com.jshoperxms.controller.utils.DateTools;
import com.jshoperxms.controller.utils.enums.BaseEnums;
import com.jshoperxms.controller.utils.enums.MemberEnums;
import com.jshoperxms.controller.utils.statickey.SerialKeys;
import com.jshoperxms.controller.utils.statickey.StaticKey;
import com.jshoperxms.dao.MemberWithdrawalsApplyTDao;
import com.jshoperxms.dao.SysMemberWithdrawalsTransferRdTDao;
import com.jshoperxms.entity.MemberT;
import com.jshoperxms.entity.MemberWithdrawalsApplyT;
import com.jshoperxms.entity.SysMemberWithdrawalsTransferRdT;
import com.jshoperxms.service.MemberWithdrawalsApplyTService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * Created by sdywcd on 16/11/20.
 * Des:
 */

@Service("memberWithdrawalsApplyTService")
@Scope("prototype")
public class MemberWithdrawalsApplyTServiceImpl extends BaseTServiceImpl<MemberWithdrawalsApplyT> implements MemberWithdrawalsApplyTService {


    @Resource
    private MemberWithdrawalsApplyTDao memberWithdrawalsApplyTDao;

    @Resource
    private SysMemberWithdrawalsTransferRdTDao sysMemberWithdrawalsTransferRdTDao;

    @Resource
    private Serial serial;

    @Transactional(propagation= Propagation.REQUIRED)
    @Override
    public void updateWithdrawalsApplyThenSaveToSysWDRDTransactional(MemberWithdrawalsApplyT rq) {
        //1如果审核通过
        if(StringUtils.equals(MemberEnums.MemberWithdrawalsReviewed.PASS.getState(),rq.getReviewed())){
           //更新审核字段pass
            MemberWithdrawalsApplyT mt=this.memberWithdrawalsApplyTDao.findByPK(MemberWithdrawalsApplyT.class,rq.getId());
            if(mt!=null){
                mt.setReviewnote(rq.getReviewnote());//审核意见
                mt.setRevieduserid(BaseTools.getAdminCreateId());
                mt.setReviewed(MemberEnums.MemberWithdrawalsReviewed.PASS.getState());
                mt.setReviewtime(DateTools.getSystemTimeBi());
                mt.setUpdatetime(DateTools.getSystemTimeBi());
                mt.setVersiont(mt.getVersiont()+1);
                this.memberWithdrawalsApplyTDao.update(mt);
                //保存转账操作记录
                SysMemberWithdrawalsTransferRdT bean=new SysMemberWithdrawalsTransferRdT();
                bean.setId(this.serial.Serialid(SerialKeys.SYSMEMBERWITHDRAWALSTRANSFERT));
                bean.setApplyaccount(mt.getApplyaccount());
                bean.setApplymemberid(mt.getApplymemberid());
                bean.setApplymembername(mt.getApplymembername());
                bean.setApplymoney(mt.getApplymoney());
                bean.setApplytime(mt.getApplytime());
                bean.setApplytype(mt.getApplytype());
                bean.setCreatetime(DateTools.getSystemTimeBi());
                bean.setCreatorid(BaseTools.getAdminCreateId());
                bean.setMemberwithdrawalsid(mt.getId());
                bean.setNote(StaticKey.EMPTY);
                bean.setStatus(BaseEnums.DataUsingState.USING.getState());
                bean.setTradeid(StaticKey.EMPTY);
                bean.setTransfermoney(BigDecimal.valueOf(0L));
                bean.setTransferpaytime(BigInteger.valueOf(0L));
                bean.setTransferstatus(MemberEnums.MemberWithdrawalsTransfer.NO.getState());
                bean.setUpdatetime(DateTools.getSystemTimeBi());
                bean.setVersiont(1);
                this.sysMemberWithdrawalsTransferRdTDao.save(bean);
            }
        }
    }
}
